home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / binutils.7 / binutils / binutils-2.7 / ld / emultempl / elf32.em < prev    next >
Encoding:
Text File  |  1996-07-15  |  26.8 KB  |  979 lines

  1. # This shell script emits a C file. -*- C -*-
  2. # It does some substitutions.
  3. # This file is now misnamed, because it supports both 32 bit and 64 bit
  4. # ELF emulations.
  5. test -z "${ELFSIZE}" && ELFSIZE=32
  6. cat >e${EMULATION_NAME}.c <<EOF
  7. /* This file is is generated by a shell script.  DO NOT EDIT! */
  8.  
  9. /* ${ELFSIZE} bit ELF emulation code for ${EMULATION_NAME}
  10.    Copyright (C) 1991, 93, 94, 95, 1996 Free Software Foundation, Inc.
  11.    Written by Steve Chamberlain <sac@cygnus.com>
  12.    ELF support by Ian Lance Taylor <ian@cygnus.com>
  13.  
  14. This file is part of GLD, the Gnu Linker.
  15.  
  16. This program is free software; you can redistribute it and/or modify
  17. it under the terms of the GNU General Public License as published by
  18. the Free Software Foundation; either version 2 of the License, or
  19. (at your option) any later version.
  20.  
  21. This program is distributed in the hope that it will be useful,
  22. but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24. GNU General Public License for more details.
  25.  
  26. You should have received a copy of the GNU General Public License
  27. along with this program; if not, write to the Free Software
  28. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  29.  
  30. #define TARGET_IS_${EMULATION_NAME}
  31.  
  32. #include "bfd.h"
  33. #include "sysdep.h"
  34.  
  35. #include <ctype.h>
  36.  
  37. #include "bfdlink.h"
  38.  
  39. #include "ld.h"
  40. #include "ldmain.h"
  41. #include "ldemul.h"
  42. #include "ldfile.h"
  43. #include "ldmisc.h"
  44. #include "ldexp.h"
  45. #include "ldlang.h"
  46. #include "ldgram.h"
  47.  
  48. static void gld${EMULATION_NAME}_before_parse PARAMS ((void));
  49. static boolean gld${EMULATION_NAME}_open_dynamic_archive
  50.   PARAMS ((const char *, search_dirs_type *, lang_input_statement_type *));
  51. static void gld${EMULATION_NAME}_after_open PARAMS ((void));
  52. static void gld${EMULATION_NAME}_check_needed
  53.   PARAMS ((lang_input_statement_type *));
  54. static void gld${EMULATION_NAME}_stat_needed
  55.   PARAMS ((lang_input_statement_type *));
  56. static boolean gld${EMULATION_NAME}_search_needed
  57.   PARAMS ((const char *, const char *));
  58. static boolean gld${EMULATION_NAME}_try_needed PARAMS ((const char *));
  59. static void gld${EMULATION_NAME}_before_allocation PARAMS ((void));
  60. static void gld${EMULATION_NAME}_find_statement_assignment
  61.   PARAMS ((lang_statement_union_type *));
  62. static void gld${EMULATION_NAME}_find_exp_assignment PARAMS ((etree_type *));
  63. static boolean gld${EMULATION_NAME}_place_orphan
  64.   PARAMS ((lang_input_statement_type *, asection *));
  65. static void gld${EMULATION_NAME}_place_section
  66.   PARAMS ((lang_statement_union_type *));
  67. static char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile));
  68.  
  69. static void
  70. gld${EMULATION_NAME}_before_parse()
  71. {
  72.   ldfile_output_architecture = bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`;
  73.   config.dynamic_link = ${DYNAMIC_LINK-true};
  74.  
  75.   if (ldfile_output_architecture == bfd_arch_alpha) {
  76.       link_info.lprefix = "$";
  77.       link_info.lprefix_len = 1;
  78.   }
  79. }
  80.  
  81. /* Try to open a dynamic archive.  This is where we know that ELF
  82.    dynamic libraries have an extension of .so.  */
  83.  
  84. static boolean
  85. gld${EMULATION_NAME}_open_dynamic_archive (arch, search, entry)
  86.      const char *arch;
  87.      search_dirs_type *search;
  88.      lang_input_statement_type *entry;
  89. {
  90.   const char *filename;
  91.   char *string;
  92.  
  93.   if (! entry->is_archive)
  94.     return false;
  95.  
  96.   filename = entry->filename;
  97.  
  98.   string = (char *) xmalloc (strlen (search->name)
  99.                  + strlen (filename)
  100.                  + strlen (arch)
  101.                  + sizeof "/lib.so");
  102.  
  103.   sprintf (string, "%s/lib%s%s.so", search->name, filename, arch);
  104.  
  105.   if (! ldfile_try_open_bfd (string, entry))
  106.     {
  107.       free (string);
  108.       return false;
  109.     }
  110.  
  111.   entry->filename = string;
  112.  
  113.   /* We have found a dynamic object to include in the link.  The ELF
  114.      backend linker will create a DT_NEEDED entry in the .dynamic
  115.      section naming this file.  If this file includes a DT_SONAME
  116.      entry, it will be used.  Otherwise, the ELF linker will just use
  117.      the name of the file.  For an archive found by searching, like
  118.      this one, the DT_NEEDED entry should consist of just the name of
  119.      the file, without the path information used to find it.  Note
  120.      that we only need to do this if we have a dynamic object; an
  121.      archive will never be referenced by a DT_NEEDED entry.
  122.  
  123.      FIXME: This approach--using bfd_elf_set_dt_needed_name--is not
  124.      very pretty.  I haven't been able to think of anything that is
  125.      pretty, though.  */
  126.   if (bfd_check_format (entry->the_bfd, bfd_object)
  127.       && (entry->the_bfd->flags & DYNAMIC) != 0)
  128.     {
  129.       char *needed_name;
  130.  
  131.       ASSERT (entry->is_archive && entry->search_dirs_flag);
  132.       needed_name = (char *) xmalloc (strlen (filename)
  133.                       + strlen (arch)
  134.                       + sizeof "lib.so");
  135.       sprintf (needed_name, "lib%s%s.so", filename, arch);
  136.       bfd_elf_set_dt_needed_name (entry->the_bfd, needed_name);
  137.     }
  138.  
  139.   return true;
  140. }
  141.  
  142. EOF
  143. if [ "x${host}" = "x${target}" ] ; then
  144.   if [ "x${DEFAULT_EMULATION}" = "x${EMULATION_NAME}" ] ; then
  145. cat >>e${EMULATION_NAME}.c <<EOF
  146.  
  147. /* For a native linker, check the file /etc/ld.so.conf for directories
  148.    in which we may find shared libraries.  /etc/ld.so.conf is really
  149.    only meaningful on Linux, but we check it on other systems anyhow.  */
  150.  
  151. static boolean gld${EMULATION_NAME}_check_ld_so_conf PARAMS ((const char *));
  152.  
  153. static boolean
  154. gld${EMULATION_NAME}_check_ld_so_conf (name)
  155.      const char *name;
  156. {
  157.   static boolean initialized;
  158.   static char *ld_so_conf;
  159.  
  160.   if (! initialized)
  161.     {
  162.       FILE *f;
  163.  
  164.       f = fopen ("/etc/ld.so.conf", FOPEN_RT);
  165.       if (f != NULL)
  166.     {
  167.       char *b;
  168.       size_t len, alloc;
  169.       int c;
  170.  
  171.       len = 0;
  172.       alloc = 100;
  173.       b = (char *) xmalloc (alloc);
  174.  
  175.       while ((c = getc (f)) != EOF)
  176.         {
  177.           if (len + 1 >= alloc)
  178.         {
  179.           alloc *= 2;
  180.           b = (char *) xrealloc (b, alloc);
  181.         }
  182.           if (c != ':'
  183.           && c != ' '
  184.           && c != '\t'
  185.           && c != '\n'
  186.           && c != ',')
  187.         {
  188.           b[len] = c;
  189.           ++len;
  190.         }
  191.           else
  192.         {
  193.           if (len > 0 && b[len - 1] != ':')
  194.             {
  195.               b[len] = ':';
  196.               ++len;
  197.             }
  198.         }
  199.         }
  200.  
  201.       if (len > 0 && b[len - 1] == ':')
  202.         --len;
  203.  
  204.       if (len > 0)
  205.         b[len] = '\0';
  206.       else
  207.         {
  208.           free (b);
  209.           b = NULL;
  210.         }
  211.  
  212.       fclose (f);
  213.  
  214.       ld_so_conf = b;
  215.     }
  216.  
  217.       initialized = true;
  218.     }
  219.  
  220.   if (ld_so_conf == NULL)
  221.     return false;
  222.  
  223.   return gld${EMULATION_NAME}_search_needed (ld_so_conf, name);
  224. }
  225.  
  226. EOF
  227.   fi
  228. fi
  229. cat >>e${EMULATION_NAME}.c <<EOF
  230.  
  231. /* These variables are required to pass information back and forth
  232.    between after_open and check_needed and stat_needed.  */
  233.  
  234. static struct bfd_link_needed_list *global_needed;
  235. static struct stat global_stat;
  236. static boolean global_found;
  237.  
  238. /* This is called after all the input files have been opened.  */
  239.  
  240. static void
  241. gld${EMULATION_NAME}_after_open ()
  242. {
  243.   struct bfd_link_needed_list *needed, *l;
  244.  
  245.   /* We only need to worry about this when doing a final link.  */
  246.   if (link_info.relocateable || link_info.shared)
  247.     return;
  248.  
  249.   /* Get the list of files which appear in DT_NEEDED entries in
  250.      dynamic objects included in the link (often there will be none).
  251.      For each such file, we want to track down the corresponding
  252.      library, and include the symbol table in the link.  This is what
  253.      the runtime dynamic linker will do.  Tracking the files down here
  254.      permits one dynamic object to include another without requiring
  255.      special action by the person doing the link.  Note that the
  256.      needed list can actually grow while we are stepping through this
  257.      loop.  */
  258.   needed = bfd_elf_get_needed_list (output_bfd, &link_info);
  259.   for (l = needed; l != NULL; l = l->next)
  260.     {
  261.       struct bfd_link_needed_list *ll;
  262.       const char *lib_path;
  263.       size_t len;
  264.       search_dirs_type *search;
  265.  
  266.       /* If we've already seen this file, skip it.  */
  267.       for (ll = needed; ll != l; ll = ll->next)
  268.     if (strcmp (ll->name, l->name) == 0)
  269.       break;
  270.       if (ll != l)
  271.     continue;
  272.  
  273.       /* See if this file was included in the link explicitly.  */
  274.       global_needed = l;
  275.       global_found = false;
  276.       lang_for_each_input_file (gld${EMULATION_NAME}_check_needed);
  277.       if (global_found)
  278.     continue;
  279.  
  280.       /* We need to find this file and include the symbol table.  We
  281.      want to search for the file in the same way that the dynamic
  282.      linker will search.  That means that we want to use
  283.      rpath_link, rpath, then the environment variable
  284.      LD_LIBRARY_PATH (native only), then the linker script
  285.      LIB_SEARCH_DIRS.  We do not search using the -L arguments.  */
  286.       if (gld${EMULATION_NAME}_search_needed (command_line.rpath_link,
  287.                           l->name))
  288.     continue;
  289.       if (gld${EMULATION_NAME}_search_needed (command_line.rpath, l->name))
  290.     continue;
  291.       if (command_line.rpath_link == NULL
  292.       && command_line.rpath == NULL)
  293.     {
  294.       lib_path = (const char *) getenv ("LD_RUN_PATH");
  295.       if (gld${EMULATION_NAME}_search_needed (lib_path, l->name))
  296.         continue;
  297.     }
  298. EOF
  299. if [ "x${host}" = "x${target}" ] ; then
  300.   if [ "x${DEFAULT_EMULATION}" = "x${EMULATION_NAME}" ] ; then
  301. cat >>e${EMULATION_NAME}.c <<EOF
  302.       lib_path = (const char *) getenv ("LD_LIBRARY_PATH");
  303.       if (gld${EMULATION_NAME}_search_needed (lib_path, l->name))
  304.     continue;
  305. EOF
  306.   fi
  307. fi
  308. cat >>e${EMULATION_NAME}.c <<EOF
  309.       len = strlen (l->name);
  310.       for (search = search_head; search != NULL; search = search->next)
  311.     {
  312.       char *filename;
  313.  
  314.       if (search->cmdline)
  315.         continue;
  316.       filename = (char *) xmalloc (strlen (search->name) + len + 2);
  317.       sprintf (filename, "%s/%s", search->name, l->name);
  318.       if (gld${EMULATION_NAME}_try_needed (filename))
  319.         break;
  320.       free (filename);
  321.     }
  322.       if (search != NULL)
  323.     continue;
  324. EOF
  325. if [ "x${host}" = "x${target}" ] ; then
  326.   if [ "x${DEFAULT_EMULATION}" = "x${EMULATION_NAME}" ] ; then
  327. cat >>e${EMULATION_NAME}.c <<EOF
  328.       if (gld${EMULATION_NAME}_check_ld_so_conf (l->name))
  329.     continue;
  330. EOF
  331.   fi
  332. fi
  333. cat >>e${EMULATION_NAME}.c <<EOF
  334.  
  335.       einfo ("%P: warning: %s, needed by %B, not found\n",
  336.          l->name, l->by);
  337.     }
  338. }
  339.  
  340. /* Search for a needed file in a path.  */
  341.  
  342. static boolean
  343. gld${EMULATION_NAME}_search_needed (path, name)
  344.      const char *path;
  345.      const char *name;
  346. {
  347.   const char *s;
  348.   size_t len;
  349.  
  350.   if (path == NULL || *path == '\0')
  351.     return false;
  352.   len = strlen (name);
  353.   while (1)
  354.     {
  355.       char *filename, *sset;
  356.  
  357.       s = strchr (path, ':');
  358.       if (s == NULL)
  359.     s = path + strlen (path);
  360.  
  361.       filename = (char *) xmalloc (s - path + len + 2);
  362.       if (s == path)
  363.     sset = filename;
  364.       else
  365.     {
  366.       memcpy (filename, path, s - path);
  367.       filename[s - path] = '/';
  368.       sset = filename + (s - path) + 1;
  369.     }
  370.       strcpy (sset, name);
  371.  
  372.       if (gld${EMULATION_NAME}_try_needed (filename))
  373.     return true;
  374.  
  375.       free (filename);
  376.  
  377.       if (*s == '\0')
  378.     break;
  379.       path = s + 1;
  380.     }
  381.  
  382.   return false;      
  383. }
  384.  
  385. /* This function is called for each possible name for a dynamic object
  386.    named by a DT_NEEDED entry.  */
  387.  
  388. static boolean
  389. gld${EMULATION_NAME}_try_needed (name)
  390.      const char *name;
  391. {
  392.   bfd *abfd;
  393.  
  394.   abfd = bfd_openr (name, bfd_get_target (output_bfd));
  395.   if (abfd == NULL)
  396.     return false;
  397.   if (! bfd_check_format (abfd, bfd_object))
  398.     {
  399.       (void) bfd_close (abfd);
  400.       return false;
  401.     }
  402.   if ((bfd_get_file_flags (abfd) & DYNAMIC) == 0)
  403.     {
  404.       (void) bfd_close (abfd);
  405.       return false;
  406.     }
  407.  
  408.   /* We've found a dynamic object matching the DT_NEEDED entry.  */
  409.  
  410.   /* We have already checked that there is no other input file of the
  411.      same name.  We must now check again that we are not including the
  412.      same file twice.  We need to do this because on many systems
  413.      libc.so is a symlink to, e.g., libc.so.1.  The SONAME entry will
  414.      reference libc.so.1.  If we have already included libc.so, we
  415.      don't want to include libc.so.1 if they are the same file, and we
  416.      can only check that using stat.  */
  417.  
  418.   if (bfd_stat (abfd, &global_stat) != 0)
  419.     einfo ("%F%P:%B: bfd_stat failed: %E\n", abfd);
  420.   global_found = false;
  421.   lang_for_each_input_file (gld${EMULATION_NAME}_stat_needed);
  422.   if (global_found)
  423.     {
  424.       /* Return true to indicate that we found the file, even though
  425.          we aren't going to do anything with it.  */
  426.       return true;
  427.     }
  428.  
  429.   /* Tell the ELF backend that don't want the output file to have a
  430.      DT_NEEDED entry for this file.  */
  431.   bfd_elf_set_dt_needed_name (abfd, "");
  432.  
  433.   /* Add this file into the symbol table.  */
  434.   if (! bfd_link_add_symbols (abfd, &link_info))
  435.     einfo ("%F%B: could not read symbols: %E\n", abfd);
  436.  
  437.   return true;
  438. }
  439.  
  440. /* See if an input file matches a DT_NEEDED entry by name.  */
  441.  
  442. static void
  443. gld${EMULATION_NAME}_check_needed (s)
  444.      lang_input_statement_type *s;
  445. {
  446.   if (global_found)
  447.     return;
  448.  
  449.   if (s->filename != NULL
  450.       && strcmp (s->filename, global_needed->name) == 0)
  451.     {
  452.       global_found = true;
  453.       return;
  454.     }
  455.  
  456.   if (s->the_bfd != NULL)
  457.     {
  458.       const char *soname;
  459.  
  460.       soname = bfd_elf_get_dt_soname (s->the_bfd);
  461.       if (soname != NULL
  462.       && strcmp (soname, global_needed->name) == 0)
  463.     {
  464.       global_found = true;
  465.       return;
  466.     }
  467.     }
  468.       
  469.   if (s->search_dirs_flag
  470.       && s->filename != NULL
  471.       && strchr (global_needed->name, '/') == NULL)
  472.     {
  473.       const char *f;
  474.  
  475.       f = strrchr (s->filename, '/');
  476.       if (f != NULL
  477.       && strcmp (f + 1, global_needed->name) == 0)
  478.     {
  479.       global_found = true;
  480.       return;
  481.     }
  482.     }
  483. }
  484.  
  485. /* See if an input file matches a DT_NEEDED entry by running stat on
  486.    the file.  */
  487.  
  488. static void
  489. gld${EMULATION_NAME}_stat_needed (s)
  490.      lang_input_statement_type *s;
  491. {
  492.   struct stat st;
  493.   const char *suffix;
  494.   const char *soname;
  495.   const char *f;
  496.  
  497.   if (global_found)
  498.     return;
  499.   if (s->the_bfd == NULL)
  500.     return;
  501.  
  502.   if (bfd_stat (s->the_bfd, &st) != 0)
  503.     {
  504.       einfo ("%P:%B: bfd_stat failed: %E\n", s->the_bfd);
  505.       return;
  506.     }
  507.  
  508.   if (st.st_dev == global_stat.st_dev
  509.       && st.st_ino == global_stat.st_ino)
  510.     {
  511.       global_found = true;
  512.       return;
  513.     }
  514.  
  515.   /* We issue a warning if it looks like we are including two
  516.      different versions of the same shared library.  For example,
  517.      there may be a problem if -lc picks up libc.so.6 but some other
  518.      shared library has a DT_NEEDED entry of libc.so.5.  This is a
  519.      hueristic test, and it will only work if the name looks like
  520.      NAME.so.VERSION.  FIXME: Depending on file names is error-prone.
  521.      If we really want to issue warnings about mixing version numbers
  522.      of shared libraries, we need to find a better way.  */
  523.  
  524.   if (strchr (global_needed->name, '/') != NULL)
  525.     return;
  526.   suffix = strstr (global_needed->name, ".so.");
  527.   if (suffix == NULL)
  528.     return;
  529.   suffix += sizeof ".so." - 1;
  530.  
  531.   soname = bfd_elf_get_dt_soname (s->the_bfd);
  532.   if (soname == NULL)
  533.     soname = s->filename;
  534.  
  535.   f = strrchr (soname, '/');
  536.   if (f != NULL)
  537.     ++f;
  538.   else
  539.     f = soname;
  540.  
  541.   if (strncmp (f, global_needed->name, suffix - global_needed->name) == 0)
  542.     einfo ("%P: warning: %s, needed by %B, may conflict with %s\n",
  543.        global_needed->name, global_needed->by, f);
  544. }
  545.  
  546. /* This is called after the sections have been attached to output
  547.    sections, but before any sizes or addresses have been set.  */
  548.  
  549. static void
  550. gld${EMULATION_NAME}_before_allocation ()
  551. {
  552.   const char *rpath;
  553.   asection *sinterp;
  554.  
  555.   /* If we are going to make any variable assignments, we need to let
  556.      the ELF backend know about them in case the variables are
  557.      referred to by dynamic objects.  */
  558.   lang_for_each_statement (gld${EMULATION_NAME}_find_statement_assignment);
  559.  
  560.   /* Let the ELF backend work out the sizes of any sections required
  561.      by dynamic linking.  */
  562.   rpath = command_line.rpath;
  563.   if (rpath == NULL)
  564.     rpath = (const char *) getenv ("LD_RUN_PATH");
  565.   if (! bfd_elf${ELFSIZE}_size_dynamic_sections (output_bfd,
  566.                          command_line.soname,
  567.                          rpath,
  568.                          command_line.export_dynamic,
  569.                          &link_info,
  570.                          &sinterp))
  571.     einfo ("%P%F: failed to set dynamic section sizes: %E\n");
  572.  
  573.   /* Let the user override the dynamic linker we are using.  */
  574.   if (command_line.interpreter != NULL
  575.       && sinterp != NULL)
  576.     {
  577.       sinterp->contents = (bfd_byte *) command_line.interpreter;
  578.       sinterp->_raw_size = strlen (command_line.interpreter) + 1;
  579.     }
  580.  
  581.   /* Look for any sections named .gnu.warning.  As a GNU extensions,
  582.      we treat such sections as containing warning messages.  We print
  583.      out the warning message, and then zero out the section size so
  584.      that it does not get copied into the output file.  */
  585.  
  586.   {
  587.     LANG_FOR_EACH_INPUT_STATEMENT (is)
  588.       {
  589.     asection *s;
  590.     bfd_size_type sz;
  591.     char *msg;
  592.     boolean ret;
  593.  
  594.     if (is->just_syms_flag)
  595.       continue;
  596.  
  597.     s = bfd_get_section_by_name (is->the_bfd, ".gnu.warning");
  598.     if (s == NULL)
  599.       continue;
  600.  
  601.     sz = bfd_section_size (is->the_bfd, s);
  602.     msg = xmalloc ((size_t) sz + 1);
  603.     if (! bfd_get_section_contents (is->the_bfd, s, msg, (file_ptr) 0, sz))
  604.       einfo ("%F%B: Can't read contents of section .gnu.warning: %E\n",
  605.          is->the_bfd);
  606.     msg[sz] = '\0';
  607.     ret = link_info.callbacks->warning (&link_info, msg,
  608.                         (const char *) NULL,
  609.                         is->the_bfd, (asection *) NULL,
  610.                         (bfd_vma) 0);
  611.     ASSERT (ret);
  612.     free (msg);
  613.  
  614.     /* Clobber the section size, so that we don't waste copying the
  615.        warning into the output file.  */
  616.     s->_raw_size = 0;
  617.       }
  618.   }
  619.  
  620. #if defined (TARGET_IS_elf32bmip) || defined (TARGET_IS_elf32lmip)
  621.   /* For MIPS ELF the .reginfo section requires special handling.
  622.      Each input section is 24 bytes, and the final output section must
  623.      also be 24 bytes.  We handle this by clobbering all but the first
  624.      input section size to 0.  The .reginfo section is handled
  625.      specially by the backend code anyhow.  */
  626.   {
  627.     boolean found = false;
  628.     LANG_FOR_EACH_INPUT_STATEMENT (is)
  629.       {
  630.     asection *s;
  631.  
  632.     if (is->just_syms_flag)
  633.       continue;
  634.  
  635.     s = bfd_get_section_by_name (is->the_bfd, ".reginfo");
  636.     if (s == NULL)
  637.       continue;
  638.  
  639.     if (! found)
  640.       {
  641.         found = true;
  642.         continue;
  643.       }
  644.  
  645.     s->_raw_size = 0;
  646.     s->_cooked_size = 0;
  647.       }
  648.   }
  649. #endif
  650. }
  651.  
  652. /* This is called by the before_allocation routine via
  653.    lang_for_each_statement.  It locates any assignment statements, and
  654.    tells the ELF backend about them, in case they are assignments to
  655.    symbols which are referred to by dynamic objects.  */
  656.  
  657. static void
  658. gld${EMULATION_NAME}_find_statement_assignment (s)
  659.      lang_statement_union_type *s;
  660. {
  661.   if (s->header.type == lang_assignment_statement_enum)
  662.     gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
  663. }
  664.  
  665. /* Look through an expression for an assignment statement.  */
  666.  
  667. static void
  668. gld${EMULATION_NAME}_find_exp_assignment (exp)
  669.      etree_type *exp;
  670. {
  671.   struct bfd_link_hash_entry *h;
  672.  
  673.   switch (exp->type.node_class)
  674.     {
  675.     case etree_provide:
  676.       h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst,
  677.                 false, false, false);
  678.       if (h == NULL)
  679.     break;
  680.  
  681.       /* We call record_link_assignment even if the symbol is defined.
  682.      This is because if it is defined by a dynamic object, we
  683.      actually want to use the value defined by the linker script,
  684.      not the value from the dynamic object (because we are setting
  685.      symbols like etext).  If the symbol is defined by a regular
  686.      object, then, as it happens, calling record_link_assignment
  687.      will do no harm.  */
  688.  
  689.       /* Fall through.  */
  690.     case etree_assign:
  691.       if (strcmp (exp->assign.dst, ".") != 0)
  692.     {
  693.       if (! (bfd_elf${ELFSIZE}_record_link_assignment
  694.          (output_bfd, &link_info, exp->assign.dst,
  695.           exp->type.node_class == etree_provide ? true : false)))
  696.         einfo ("%P%F: failed to record assignment to %s: %E\n",
  697.            exp->assign.dst);
  698.     }
  699.       gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
  700.       break;
  701.  
  702.     case etree_binary:
  703.       gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
  704.       gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
  705.       break;
  706.  
  707.     case etree_trinary:
  708.       gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
  709.       gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
  710.       gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
  711.       break;
  712.  
  713.     case etree_unary:
  714.       gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
  715.       break;
  716.  
  717.     default:
  718.       break;
  719.     }
  720. }
  721.  
  722. /* Place an orphan section.  We use this to put random SHF_ALLOC
  723.    sections in the right segment.  */
  724.  
  725. static asection *hold_section;
  726. static lang_output_section_statement_type *hold_use;
  727. static lang_output_section_statement_type *hold_text;
  728. static lang_output_section_statement_type *hold_rodata;
  729. static lang_output_section_statement_type *hold_data;
  730. static lang_output_section_statement_type *hold_bss;
  731. static lang_output_section_statement_type *hold_rel;
  732.  
  733. /*ARGSUSED*/
  734. static boolean
  735. gld${EMULATION_NAME}_place_orphan (file, s)
  736.      lang_input_statement_type *file;
  737.      asection *s;
  738. {
  739.   lang_output_section_statement_type *place;
  740.   asection *snew, **pps;
  741.   lang_statement_list_type *old;
  742.   lang_statement_list_type add;
  743.   etree_type *address;
  744.   const char *secname, *ps;
  745.   lang_output_section_statement_type *os;
  746.  
  747.   if ((s->flags & SEC_ALLOC) == 0)
  748.     return false;
  749.  
  750.   /* Look through the script to see where to place this section.  */
  751.   hold_section = s;
  752.   hold_use = NULL;
  753.   lang_for_each_statement (gld${EMULATION_NAME}_place_section);
  754.  
  755.   if (hold_use != NULL)
  756.     {
  757.       /* We have already placed a section with this name.  */
  758.       wild_doit (&hold_use->children, s, hold_use, file);
  759.       return true;
  760.     }
  761.  
  762.   secname = bfd_get_section_name (s->owner, s);
  763.  
  764.   /* If this is a final link, then always put .gnu.warning.SYMBOL
  765.      sections into the .text section to get them out of the way.  */
  766.   if (! link_info.shared
  767.       && ! link_info.relocateable
  768.       && strncmp (secname, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0
  769.       && hold_text != NULL)
  770.     {
  771.       wild_doit (&hold_text->children, s, hold_text, file);
  772.       return true;
  773.     }
  774.  
  775.   /* Decide which segment the section should go in based on the
  776.      section name and section flags.  */
  777.   place = NULL;
  778.   if ((s->flags & SEC_HAS_CONTENTS) == 0
  779.       && hold_bss != NULL)
  780.     place = hold_bss;
  781.   else if ((s->flags & SEC_READONLY) == 0
  782.        && hold_data != NULL)
  783.     place = hold_data;
  784.   else if (strncmp (secname, ".rel", 4) == 0
  785.        && hold_rel != NULL)
  786.     place = hold_rel;
  787.   else if ((s->flags & SEC_CODE) == 0
  788.        && (s->flags & SEC_READONLY) != 0
  789.        && hold_rodata != NULL)
  790.     place = hold_rodata;
  791.   else if ((s->flags & SEC_READONLY) != 0
  792.        && hold_text != NULL)
  793.     place = hold_text;
  794.   if (place == NULL)
  795.     return false;
  796.  
  797.   /* Create the section in the output file, and put it in the right
  798.      place.  This shuffling is to make the output file look neater.  */
  799.   snew = bfd_make_section (output_bfd, secname);
  800.   if (snew == NULL)
  801.       einfo ("%P%F: output format %s cannot represent section called %s\n",
  802.          output_bfd->xvec->name, secname);
  803.   if (place->bfd_section != NULL)
  804.     {
  805.       for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next)
  806.     ;
  807.       *pps = snew->next;
  808.       snew->next = place->bfd_section->next;
  809.       place->bfd_section->next = snew;
  810.     }
  811.  
  812.   /* Start building a list of statements for this section.  */
  813.   old = stat_ptr;
  814.   stat_ptr = &add;
  815.   lang_list_init (stat_ptr);
  816.  
  817.   /* If the name of the section is representable in C, then create
  818.      symbols to mark the start and the end of the section.  */
  819.   for (ps = secname; *ps != '\0'; ps++)
  820.     if (! isalnum (*ps) && *ps != '_')
  821.       break;
  822.   if (*ps == '\0' && config.build_constructors)
  823.     {
  824.       char *symname;
  825.  
  826.       symname = (char *) xmalloc (ps - secname + sizeof "__start_");
  827.       sprintf (symname, "__start_%s", secname);
  828.       lang_add_assignment (exp_assop ('=', symname,
  829.                       exp_unop (ALIGN_K,
  830.                         exp_intop ((bfd_vma) 1
  831.                                << s->alignment_power))));
  832.     }
  833.  
  834.   if (! link_info.relocateable)
  835.     address = NULL;
  836.   else
  837.     address = exp_intop ((bfd_vma) 0);
  838.  
  839.   lang_enter_output_section_statement (secname, address, 0,
  840.                        (bfd_vma) 0,
  841.                        (etree_type *) NULL,
  842.                        (etree_type *) NULL,
  843.                        (etree_type *) NULL);
  844.  
  845.   os = lang_output_section_statement_lookup (secname);
  846.   wild_doit (&os->children, s, os, file);
  847.  
  848.   lang_leave_output_section_statement ((bfd_vma) 0, "*default*");
  849.   stat_ptr = &add;
  850.  
  851.   if (*ps == '\0' && config.build_constructors)
  852.     {
  853.       char *symname;
  854.  
  855.       symname = (char *) xmalloc (ps - secname + sizeof "__stop_");
  856.       sprintf (symname, "__stop_%s", secname);
  857.       lang_add_assignment (exp_assop ('=', symname,
  858.                       exp_nameop (NAME, ".")));
  859.     }
  860.  
  861.   /* Now stick the new statement list right after PLACE.  */
  862.   *add.tail = place->header.next;
  863.   place->header.next = add.head;
  864.  
  865.   stat_ptr = old;
  866.  
  867.   return true;
  868. }
  869.  
  870. static void
  871. gld${EMULATION_NAME}_place_section (s)
  872.      lang_statement_union_type *s;
  873. {
  874.   lang_output_section_statement_type *os;
  875.  
  876.   if (s->header.type != lang_output_section_statement_enum)
  877.     return;
  878.  
  879.   os = &s->output_section_statement;
  880.  
  881.   if (strcmp (os->name, hold_section->name) == 0)
  882.     hold_use = os;
  883.  
  884.   if (strcmp (os->name, ".text") == 0)
  885.     hold_text = os;
  886.   else if (strcmp (os->name, ".rodata") == 0)
  887.     hold_rodata = os;
  888.   else if (strcmp (os->name, ".data") == 0)
  889.     hold_data = os;
  890.   else if (strcmp (os->name, ".bss") == 0)
  891.     hold_bss = os;
  892.   else if (hold_rel == NULL
  893.        && os->bfd_section != NULL
  894.        && strncmp (os->name, ".rel", 4) == 0)
  895.     hold_rel = os;
  896. }
  897.  
  898. static char *
  899. gld${EMULATION_NAME}_get_script(isfile)
  900.      int *isfile;
  901. EOF
  902.  
  903. if test -n "$COMPILE_IN"
  904. then
  905. # Scripts compiled in.
  906.  
  907. # sed commands to quote an ld script as a C string.
  908. sc='s/["\\]/\\&/g
  909. s/$/\\n\\/
  910. 1s/^/"/
  911. $s/$/n"/
  912. '
  913.  
  914. cat >>e${EMULATION_NAME}.c <<EOF
  915. {                 
  916.   *isfile = 0;
  917.  
  918.   if (link_info.relocateable == true && config.build_constructors == true)
  919.     return `sed "$sc" ldscripts/${EMULATION_NAME}.xu`;
  920.   else if (link_info.relocateable == true)
  921.     return `sed "$sc" ldscripts/${EMULATION_NAME}.xr`;
  922.   else if (!config.text_read_only)
  923.     return `sed "$sc" ldscripts/${EMULATION_NAME}.xbn`;
  924.   else if (!config.magic_demand_paged)
  925.     return `sed "$sc" ldscripts/${EMULATION_NAME}.xn`;
  926.   else if (link_info.shared)
  927.     return `sed "$sc" ldscripts/${EMULATION_NAME}.xs`;
  928.   else
  929.     return `sed "$sc" ldscripts/${EMULATION_NAME}.x`;
  930. }
  931. EOF
  932.  
  933. else
  934. # Scripts read from the filesystem.
  935.  
  936. cat >>e${EMULATION_NAME}.c <<EOF
  937. {                 
  938.   *isfile = 1;
  939.  
  940.   if (link_info.relocateable == true && config.build_constructors == true)
  941.     return "ldscripts/${EMULATION_NAME}.xu";
  942.   else if (link_info.relocateable == true)
  943.     return "ldscripts/${EMULATION_NAME}.xr";
  944.   else if (!config.text_read_only)
  945.     return "ldscripts/${EMULATION_NAME}.xbn";
  946.   else if (!config.magic_demand_paged)
  947.     return "ldscripts/${EMULATION_NAME}.xn";
  948.   else if (link_info.shared)
  949.     return "ldscripts/${EMULATION_NAME}.xs";
  950.   else
  951.     return "ldscripts/${EMULATION_NAME}.x";
  952. }
  953. EOF
  954.  
  955. fi
  956.  
  957. cat >>e${EMULATION_NAME}.c <<EOF
  958.  
  959. struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation = 
  960. {
  961.   gld${EMULATION_NAME}_before_parse,
  962.   syslib_default,
  963.   hll_default,
  964.   after_parse_default,
  965.   gld${EMULATION_NAME}_after_open,
  966.   after_allocation_default,
  967.   set_output_arch_default,
  968.   ldemul_default_target,
  969.   gld${EMULATION_NAME}_before_allocation,
  970.   gld${EMULATION_NAME}_get_script,
  971.   "${EMULATION_NAME}",
  972.   "${OUTPUT_FORMAT}",
  973.   NULL,
  974.   NULL,
  975.   gld${EMULATION_NAME}_open_dynamic_archive,
  976.   gld${EMULATION_NAME}_place_orphan
  977. };
  978. EOF
  979.